/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PHS;

import java.sql.*;
import java.io.*;
import java.util.*;
public class MedicinePrescribe implements Serializable {
    String url="jdbc:db2://localhOst:50000/PHSYSTEM";
    String driver="com.ibm.db2.jcc.DB2Driver";
    String user="db2admin";
    String pwd="db2admin";
    int rowCount;
    Statement stmt;
    ResultSet rs;
    PreparedStatement pstmt;
    Connection conn=null;
    String medicineName;
    String medicineType;
    String usage;
    String dose;
    int quantity;
    int patientId;
    int doctorId;
    ArrayList nameList=new ArrayList();
    ArrayList doseList=new ArrayList();
    ArrayList usageList=new ArrayList();
    ArrayList typeList=new ArrayList();
    ArrayList qntityList=new ArrayList();
   
public MedicinePrescribe()
{
   try{
        Class.forName(driver);
        conn=DriverManager.getConnection(url,user,pwd);
        stmt=conn.createStatement();
        System.out.println("connection :"+conn);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


public void setMedicineName(String medicineName)
{
    this.medicineName=medicineName;
  System.out.println("setter method for MedicineName"+this.medicineName);

    }
public void setPatientId(int patientId)
{
    this.patientId=patientId;
    System.out.println("Settr method for PatientId:"+patientId);
}
public void setDoctorId(int doctorId)
{
    this.doctorId=doctorId;
    System.out.println("Settr method for Doctor:"+doctorId);
}
public void setMedicineType(String medicineType)
{
    this.medicineType=medicineType;
    System.out.println("setter method for Medicinetype"+this.medicineType);

    }
public void setUsage(String usage)
{
    this.usage=usage;
    System.out.println("setter method for usage"+this.usage);
}
public void setDose(String dose)
{
    this.dose=dose;
    System.out.println("setter method for dose"+this.dose);
}
public void setQuantity(int quantity)
{
    this.quantity=quantity;
    System.out.println("setter method for quantity"+this.quantity);
}
public int medicineInsert()
{
    try
    {
    String insert="INSERT INTO MEDICINE(PatientId,DoctorId,MedicineName,Type,Usage,Dose,PQuantity,Date,Status)"+
                  " VALUES(?,?,?,?,?,?,?,CURRENT DATE,'TO BE ISSUED')";
     conn.setAutoCommit(false);
     pstmt=conn.prepareStatement(insert);
        pstmt.setInt(1,patientId);
        pstmt.setInt(2,doctorId);
        pstmt.setString(3,medicineName);
        pstmt.setString(4,medicineType);
        pstmt.setString(5,usage);
        pstmt.setString(6,dose);
        pstmt.setInt(7,quantity);
        rowCount=pstmt.executeUpdate();
                conn.commit();
        }
    catch(SQLException e)
    {
        e.printStackTrace();
    }
    return rowCount;
}
public void getMedicinePrescribed()
{
    String query="SELECT MedicineName,Usage,Dose,Type,PQuantity " +
            "FROM Medicine WHERE Status='TO BE ISSUED' AND PatientId="+patientId;
    try{
        rs=stmt.executeQuery(query);
        while(rs.next())
        {
            nameList.add(rs.getString("MedicineName"));
            usageList.add(rs.getString("usage"));
            doseList.add(rs.getString("Dose"));
            typeList.add(rs.getString("Type"));
            qntityList.add(rs.getString("PQuantity"));
        }
    }catch(SQLException e)
    {
        e.printStackTrace();
    }

}
    public ArrayList getnameList()
    {
        System.out.println(nameList);
        return nameList;
    }
    public ArrayList getDoseList()
    {
        return doseList;
    }
    public ArrayList getUsageList()
    {
        return usageList;
    }
    public ArrayList getQuantityList()
    {
        return qntityList;
    }
    public ArrayList getTypeList()
    {
        return typeList;
    }
}

